package com.ruoyi.teaching.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.teaching.domain.Classes;
import com.ruoyi.teaching.mapper.ClassesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teaching.mapper.StudentMapper;
import com.ruoyi.teaching.domain.Student;
import com.ruoyi.teaching.service.IStudentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 学生信息Service业务层处理
 *
 * @author teaching
 * @date 2024-01-20
 */
@Service
public class StudentServiceImpl implements IStudentService
{
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClassesMapper classesMapper;
    @Autowired
    private RemoteUserService remoteUserService;

    /**
     * 查询学生信息
     *
     * @param id 学生信息主键
     * @return 学生信息
     */
    @Override
    public Student selectStudentById(Long id)
    {
        return studentMapper.selectStudentById(id);
    }

    /**
     * 查询学生信息列表
     *
     * @param student 学生信息
     * @return 学生信息
     */
    @Override
    public List<Student> selectStudentList(Student student)
    {
        return studentMapper.selectStudentList(student);
    }

    /**
     * 新增学生信息
     *
     * @param student 学生信息
     * @return 结果
     */
    @Override
    //TODO分布式事务
    public int insertStudent(Student student)
    {

//        远程新增一个账号，返回userid
        SysUser sysUser = new SysUser();
        sysUser.setUserName(student.getUsername());
        sysUser.setPhonenumber(student.getContactNumber());
        sysUser.setNickName(student.getName());
        sysUser.setRoleId(3L);
        sysUser.setSex(String.valueOf(student.getGender()));
        sysUser.setStatus("0");
        sysUser.setCreateBy(SecurityUtils.getUsername());
        sysUser.setCreateTime(new Date());
//        密码加密
        sysUser.setPassword(SecurityUtils.encryptPassword("123456"));
        R<Long> r = remoteUserService.registerUserStudentInfo(sysUser);
        if (r.getData()!=null){
            Long userId = r.getData();
            String graduationDate = student.getGraduationDate();
            String s = graduationDate.substring(0, 19).replaceAll("T", " ").substring(0,10);

            String birth = student.getBirth();
            String date = birth.substring(0, 19).replaceAll("T" , " " ).substring(0, 10);
            Long classesId = student.getClassesId();
            String studentName = classesMapper.selectClassesByClassId(classesId);
            student.setGraduationDate(s);
            student.setBirth(date);
            student.setClassesName(studentName);
            student.setId(userId);
            return studentMapper.insertStudent(student);
        }

        return 0;

    }

    /**
     * 修改学生信息
     *
     * @param student 学生信息
     * @return 结果
     */
    @Override
    public int updateStudent(Student student)
    {
        Long classesId = student.getClassesId();
        String studentName = classesMapper.selectClassesByClassId(classesId);
        student.setClassesName(studentName);
        return studentMapper.updateStudent(student);
    }

    /**
     * 批量删除学生信息
     *
     * @param ids 需要删除的学生信息主键
     * @return 结果
     */
    @Override
    public int deleteStudentByIds(Long[] ids)
    {
        return studentMapper.deleteStudentByIds(ids);
    }

    /**
     * 删除学生信息信息
     *
     * @param id 学生信息主键
     * @return 结果
     */
    @Override
    public int deleteStudentById(Long id)
    {
        return studentMapper.deleteStudentById(id);
    }

    @Override
    public List<Classes> selectlistClassName(String className) {
        return studentMapper.selectStudentByClassName(className);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Student> selectStudentListByClassesId(Long classesId) {
        return studentMapper.selectStudentByClassesId(classesId);
    }
}
